﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;

namespace ArbolandiaBL
{
    public class Utilities
    {
        public static string SerializeToJson<T>(T obj)
        {
            var res = "";
            try
            {
                MemoryStream stream1 = new MemoryStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                ser.WriteObject(stream1, obj);
                stream1.Position = 0;
                StreamReader sr = new StreamReader(stream1);
                res = sr.ReadToEnd();
            }
            catch (Exception)
            {
                res = "";
            }
            return res;

        }
        public static T DesSerializeJson<T>(string obj)
        {
            MemoryStream stream1 = new MemoryStream(Encoding.UTF8.GetBytes(obj));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            T res = (T)ser.ReadObject(stream1);
            return res;

        }
        [DataContract]
        public class OrderJson
        {
            [DataMember]
            public int productDetailId { get; set; }
            [DataMember]
            public int productId { get; set; }
            [DataMember]
            public int quantity { get; set; }
            [DataMember]
            public decimal amount { get; set; }
        }
        public static string STATUS_1 = "Registrado Paso 2";
        public static string STATUS_2 = "Enviado Pago DineroMail";
        public static string STATUS_3 = "Opcion Pago Por Transferencia";
        public static string STATUS_4 = "Enviado Pago PayPal";
        public static string STATUS_XX = "No se pudo determinar el estatus actual del pedido";
        public static T DeserializeObject<T>(string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);
        }
        private static Byte[] StringToUTF8ByteArray(string pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }


    }
}
